<template>
  <!-- 我的钱包 -->
  <div>
    <van-nav-bar fixed title="我的钱包" left-arrow @click-left="$utils.back($route.path);" />
    <div class="has-top">
      <div class="pd-32">
        <div class="bg-primary color-white top-box">
          <p class="fs-28">钱包余额</p>
          <p class="fs-56 mt-12 ml--10">￥{{ balance||0 }}</p>
          <div class="flex-s-c mt-20">
            <p class="fs-28 ">我的货款：{{ paymentBalance||0 }}元</p>
            <p class="fs-28 ">保证金：{{ deposit||0 }}元</p>
          </div>
          <div class="flex-s-c mt-24">
            <a class="top-btn" href="/m/user/BalanceDraw">我要提现</a>
            <a class="top-btn" href="/m/user/BalanceDrawList">提现记录</a>
          </div>
        </div>
        <div class="flex-s-c date-box">
          <span v-for="(item,index) in dateList" :key="index" class="dateItem" :class="activeIndex==index?'active':''" @click="changeTab(item,index)">{{ item.title }}</span>
          <div class="flex-s-0 filter-box flex-fe-c" @click="openDatetimeSelect"><span>筛选</span><img src="@/assets/images/wealthManage/icon-01.png" class=""></div>
        </div>
        <div v-if="list&&list.length>0" class="list">
          <div v-for="(item,index) in list" :key="index" class="item flex-s-fs">
            <div class="item-left flex-1">
              <p class="fs-30 text-show-line-1">{{ item.TypeName }}</p>
              <p class="color-gray-light mt-10">{{ item.SettlementDate }}</p>
              <p class="color-gray-light mt-10">备注信息：{{ item.Remark }}</p>
            </div>
            <div class="item-right flex-s-0 ml-20">
              <p class="fs-30 tar">{{ item.Money }}元</p>
              <p class="color-gray-light tar mt-10">结余：{{ item.Balance }}元</p>
            </div>
          </div>
        </div>
        <div v-else>
          <div class="data-empty">
            <img class="img" src="@/assets/images/wealthManage/empty.png">
            <div class="text">~ 暂无数据 ~</div>
          </div>
        </div>
      </div>
    </div>

    <datetime-select
      title="请选择开始时间"
      :show="timeStartSelect"
      @onConfirm="onStartConfirm"
      @clickOverlay="clickOverlay"
    />
    <datetime-select
      title="请选择结束时间"
      :show="timeEndSelect"
      @onConfirm="onEndConfirm"
      @clickOverlay="clickOverlay"
    />
  </div>
</template>

<script>
import datetimeSelect from 'components/datetimeSelect'
import { GetWholeSaleMoneyList, GetUerBalanceByMoneyType, GetUerApplyMoney } from 'api/member'
export default {
  components: { datetimeSelect },
  data() {
    return {
      activeIndex: 0,
      dateList: [
        { title: '全部',
          incomeOrExpenditure: -1
        },
        { title: '增加',
          incomeOrExpenditure: 1
        },
        { title: '减少',
          incomeOrExpenditure: 2
        }
      ],

      // 时间选择框显示
      timeStartSelect: false,
      timeEndSelect: false,
      balance: 0,
      paymentBalance: 0,
      deposit: 0,
      // 贷款明细
      list: [],
      totalNum: 0,

      requestData: {
        pageIndex: 1,
        pageSize: 10,
        type: 0,
        startDay: '',
        endDay: '',
        incomeOrExpenditure: -1
      },
      // 滚动状态
      totalAmout: 0,
      scollRequest: true,
      end: false
    }
  },
  created() {
    this.refresh()
    this.GetUerBalanceByMoneyType(0)
    this.GetUerBalanceByMoneyType(2)
    this.GetUerApplyMoney()
  },
  mounted() {
    this.$nextTick(function() {
      window.addEventListener('scroll', this.onScroll)
    })
  },
  methods: {
    changeTab(item, index) {
      if (this.activeIndex != index) {
        this.activeIndex = index
        this.requestData.incomeOrExpenditure = item.incomeOrExpenditure
        // 此处重新请求接口
        this.refresh()
      }
    },
    openDatetimeSelect() {
      this.timeStartSelect = true
    },
    onStartConfirm(value) {
      this.requestData.startDay = this.$utils.dateFomat('yyyy-mm-dd', value)
      this.timeStartSelect = false
      this.timeEndSelect = true
    },
    onEndConfirm(value) {
      this.requestData.endDay = this.$utils.dateFomat('yyyy-mm-dd', value)
      if (this.requestData.endDay < this.requestData.startDay) {
        this.$toast({
          message: '结束时间不得小于开始时间，请重新选择'
        })
        return
      }
      this.timeStartSelect = false
      this.timeEndSelect = false
      this.refresh()
    },
    resetParam() {
      this.requestData.pageIndex = 1
      this.requestData.pageSize = 10
      this.totalAmout = 0
      this.scollRequest = true
      this.end = false
      this.list = []
      this.totalNum = 0
    },
    refresh() {
      this.resetParam()
      this.GetWholeSaleMoneyList(this.requestData)
    },
    clickOverlay() {
      this.timeStartSelect = false
      this.timeEndSelect = false
    },
    GetUerBalanceByMoneyType(type) {
      GetUerBalanceByMoneyType({ type }).then(res => {
        if (res) {
          if (type == 0) {
            this.balance = res.Data
          } else if (type == 2) {
            this.paymentBalance = res.Data
          }
        }
      })
    },
    GetUerApplyMoney() {
      GetUerApplyMoney().then(res => {
        if (res) {
          this.deposit = res.Data
        }
      })
    },

    GetWholeSaleMoneyList() {
      GetWholeSaleMoneyList(this.requestData).then(res => {
        if (res) {
          if (res.Data.length && res.Data.length > 0) {
            res.Data.forEach(tem => {
              if (this.$WeikeRechargeRewardName != '微客充值奖') {
                tem.TypeName = tem.TypeName.replace('微客充值奖', this.$WeikeRechargeRewardName)
                tem.Remark = tem.Remark.replace('微客充值奖', this.$WeikeRechargeRewardName)
              }
              this.list.push(tem)
            })
          }
          this.totalAmout = res.TotalAmout
          this.scollRequest = true
        }
      })
    },

    // 滚动事件触发下拉加载
    onScroll() {
      const _this = this
      if (_this.scollRequest === true) {
        if (this.$utils.getScrollHeight() - this.$utils.getClientHeight() - this.$utils.getScrollTop() <= 5) {
          _this.scollRequest = false
          console.log('加载')
          if (_this.requestData.pageIndex < _this.totalAmout) {
            _this.requestData.pageIndex++
            _this.GetWholeSaleMoneyList()
          } else {
            console.log('end')
            _this.end = true
          }
          // }
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.top-box{
  padding: 56px 48px 38px;
  border-radius: 28px;
  .top-btn{
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.34);
    font-size: 28px;
    line-height: 60px;
    padding: 0 50px;
    border-radius: 30px;
  }
}

.date-box{
  position: sticky;
  top: 80px;
  padding: 32px 0 ;
  background: $colorF5;
  .dateItem{
    font-size: 28px;
    flex-shrink: 0;
    text-align: center;
    padding: 12px 40px;
    border-radius: 33px;;
  }
  .active{
    @include bg-primary;
    color:$colorF;
  }
  .filter-box{
    flex: 1;
    margin-left: 50px;
    span{
      font-size: 26px;
    }
    img{
      height: 40px;
      width: 40px;
    }
  }
}

.list{
  .item{
    background: #fff;
    border-radius: 16px;
    padding: 28px 24px;
    margin-bottom: 24px;
  }
}

.data-empty{
  text-align: center;
  .img{
    width: 400px;
    height: 300px;
  }
  .text{
    font-size: 28px;
    color: $color6;
  }
}
</style>
